﻿$(function () {
    var loadError = function () {
        //控件的一些加载错误的提示
        var easyuiOnLoadError = function (XMLHttpRequest) {
            manage.msg("出错了,已经发送至控制台");
            console.info(XMLHttpRequest.responseText);
        };
        $.fn.datagrid.defaults.onLoadError = easyuiOnLoadError;
        $.fn.treegrid.defaults.onLoadError = easyuiOnLoadError;
        $.fn.tree.defaults.onLoadError = easyuiOnLoadError;
        $.fn.combogrid.defaults.onLoadError = easyuiOnLoadError;
        $.fn.combobox.defaults.onLoadError = easyuiOnLoadError;
        $.fn.form.defaults.onLoadError = easyuiOnLoadError;
        $.fn.panel.defaults.onLoadError = easyuiOnLoadError;
    };
    var validateboxRules = function () {
        $.extend($.fn.validatebox.defaults.rules, {
            minLength: {
                validator: function (value, param) {
                    return value.length >= param[0];
                },
                message: '长度不得小于 {0} 位'
            },
            equals: {
                validator: function (value, param) {
                    return value == $(param[0]).val();
                },
                message: '两次输入内容不一致'
            },
            length: {
                validator: function (value, param) {
                    var len = $.trim(value).length;
                    return len >= param[0] && len <= param[1];
                },
                message: "内容长度介于{0}和{1}之间."
            },
            zip: {
                validator: function (value) {
                    return /^[1-9]\d{5}$/i.test(value);
                },
                message: "邮政编码格式不正确"
            },
            tel: {
                validator: function (value) {
                    return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
                },
                message: "格式不正确,请使用下面格式:010-88888888"
            },
            mobile: {
                validator: function (value) {
                    return /^(13|15|18)\d{9}$/i.test(value);
                },
                message: "手机号码格式不正确(正确格式如：13845666666)"
            },
            telOrMobile: {
                validator: function (value) {
                    return /^(13|15|18)\d{9}$/i.test(value) || /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
                },
                message: "请填入手机或电话号码,如 13845666666 或 010-8888888"
            },
            idcard: {
                validator: function (value) {
                    return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
                },
                message: "身份证号码格式不正确"
            },
            qq: {
                validator: function (value) {
                    return /^[1-9]\d{4,9}$/i.test(value);
                },
                message: "QQ号码格式不正确"
            },
            chinese: {
                validator: function (value) {
                    return /^[\u0391-\uFFE5]+$/i.test(value);
                },
                message: "请输入中文"
            },
            english: {
                validator: function (value) {
                    return /^[A-Za-z]+$/i.test(value);
                },
                message: "请输入英文"
            },
            unnormal: {
                validator: function (value) {
                    return /.+/i.test(value);
                },
                message: "输入值不能为空和包含其他非法字符"
            },
            username: {
                validator: function (value) {
                    return /^[a-zA-Z][a-zA-Z0-9_]{3,15}$/i.test(value);
                },
                message: "用户名不合法(字母开头，允许4-16字节，允许字母数字下划线)"
            },
            url: {
                validator: function (value) {
                    return /^[a-zA-z]+:\/\/[^\s]*$/i.test(value);
                },
                message: "连接地址格式不正确（http:// 或 https:// 开头）"
            }
        });
    };
    var smoothTree = function () {
        //扩展tree,使其支持平滑数据格式（SyPro）
        var easyuiTreeLoadFilter = function (data, pid) {
            var treeData = [], tmpMap = [];
            for (var i = 0; i < data.length; i++) { tmpMap[data[i]["id"]] = data[i]; }
            for (var i = 0; i < data.length; i++) {
                if (tmpMap[data[i]["pid"]]) {
                    if (!tmpMap[data[i]["pid"]]["children"]) {
                        tmpMap[data[i]["pid"]]["children"] = [];
                    }
                    tmpMap[data[i]["pid"]]["children"].push(data[i]);
                } else { treeData.push(data[i]); }
            }
            return treeData;
        };
        $.fn.combotree.defaults.loadFilter = easyuiTreeLoadFilter;
        $.fn.tree.defaults.loadFilter = easyuiTreeLoadFilter;

        //扩展treegrid,使其支持平滑数据格式（SyPro）
        var easyuiTreeGridLoadFilter = function (data, pid) {
            var opt = $(this).data().treegrid.options;
            var treeData = [], tmpMap = [];
            for (var i = 0; i < data.length; i++) { tmpMap[data[i][opt.idField]] = data[i]; }
            for (var i = 0; i < data.length; i++) {
                if (tmpMap[data[i][opt.parentField]]) {
                    if (!tmpMap[data[i][opt.parentField]]["children"]) {
                        tmpMap[data[i][opt.parentField]]["children"] = [];
                    }
                    tmpMap[data[i][opt.parentField]]["children"].push(data[i]);
                } else { treeData.push(data[i]); }
            }
            return treeData;
        };
        $.fn.treegrid.defaults.loadFilter = easyuiTreeGridLoadFilter;
    };
    var textWidth = function () {
        $.fn.validatebox.defaults.width = "100%";
        $.fn.textbox.defaults.width = "100%";
        $.fn.combo.defaults.width = "100%";
        $.fn.combobox.defaults.width = "100%";
        $.fn.combotree.defaults.width = "100%";
        $.fn.combogrid.defaults.width = "100%";
        $.fn.combotreegrid.defaults.width = "100%";
        $.fn.tagbox.defaults.width = "100%";
        $.fn.numberbox.defaults.width = "100%";
        $.fn.datebox.defaults.width = "100%";
        $.fn.datetimebox.defaults.width = "100%";
        $.fn.datetimespinner.defaults.width = "100%";
        $.fn.numberspinner.defaults.width = "100%";
        $.fn.timespinner.defaults.width = "100%";
    };

    loadError(); validateboxRules(); smoothTree(); textWidth();
});